<script setup lang="ts">
import { toLoginRoute } from '/@lib/utils/routes.ts'
import { useAuthStore } from '/@/store/modules/auth.ts'

defineOptions({
  name: 'VipUserAvatar',
})

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()
const { avatar, username } = storeToRefs(authStore)
const { logout } = authStore
const active = ref<boolean>(false)

const handleVisibleChange = (value: boolean) => {
  active.value = value
}
const handleCommand = async (command: any) => {
  switch (command) {
    case 'logout':
      await logout()
      await router.push(toLoginRoute(route.fullPath))
      break
  }
}
</script>

<template>
  <el-dropdown @command="handleCommand" @visible-change="handleVisibleChange">
    <span class="avatar-dropdown">
      <el-avatar class="user-avatar" :src="avatar" />
      <span class="vip-dot vip-dot-success"></span>
      <div class="username">
        <span class="hidden-xs-only">{{ username }}</span>
        <vip-icon class="vip-dropdown" :class="{ 'vip-dropdown-active': active }" icon="arrow-down-s-line" />
      </div>
    </span>
    <template #dropdown>
      <span class="avatar-dropdown-menu">
        <el-avatar class="user-avatar" :src="avatar" />
        <span class="vip-dot vip-dot-success"></span>
        <div class="username">
          <span class="hidden-xs-only" style="font-size: 14px">{{ username }}</span>
          <span class="hidden-xs-only" style="font-size: 10px; margin-top: 4px">个人中心</span>
        </div>
      </span>
      <el-dropdown-menu>
        <el-dropdown-item command="logout">
          <vip-icon icon="logout-circle-r-line" />
          <span>{{ '退出登录' }}</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<style scoped lang="scss">
.avatar-dropdown {
  display: flex;
  align-content: center;
  align-items: center;
  justify-content: center;
  justify-items: center;

  .user-avatar {
    box-sizing: border-box;
    width: 40px;
    height: 40px;
    padding: 8px;
    margin-left: 15px;
    cursor: pointer;
    border-radius: 50%;
  }

  .username {
    position: relative;
    display: flex;
    align-content: center;
    align-items: center;
    width: max-content;
    height: 40px;
    margin-left: 6px;
    line-height: 40px;
    cursor: pointer;

    [class*='ri-'] {
      margin-left: 0 !important;
    }
  }
}

.avatar-dropdown-menu {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  //justify-content: center;
  width: 220px;
  height: 80px;
  border-bottom: 1px solid var(--el-border-color);

  .user-avatar {
    box-sizing: border-box;
    width: 40px;
    height: 40px;
    padding: 8px;
    margin-left: 15px;
    cursor: pointer;
    border-radius: 50%;
  }

  .username {
    display: flex;
    flex-direction: column;
    //height: 40px;
    //line-height: 40px;
    margin-left: 6px;
    cursor: pointer;

    > span {
      display: inline-block;
      vertical-align: middle;
    }
  }
}
</style>
